package com.stylefeng.guns.rest.user;

import com.alibaba.dubbo.config.annotation.Reference;
import com.stylefeng.guns.rest.common.CurrentUser;
import com.stylefeng.guns.rest.vo.ResultVO;
import com.stylefeng.guns.api.user.vo.UserInfoModel;
import com.stylefeng.guns.api.user.vo.UserModel;
import com.stylefeng.guns.api.user.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference(interfaceClass = UserService.class,check = false)
    private UserService userService;

    @RequestMapping(value = "register",method = RequestMethod.POST)
    public ResultVO register(UserModel userModel){
        if(userModel.getUsername() == null || userModel.getUsername().trim().length() == 0){
            return ResultVO.serviceFail("用户名不能为空");
        }
        if(userModel.getPassword() == null || userModel.getPassword().trim().length() == 0){
            return ResultVO.serviceFail("密码不能为空");
        }
        boolean isSuccess = userService.register(userModel);
        if(isSuccess){
            return ResultVO.success("注册成功");
        }else{
            return ResultVO.serviceFail("注册失败");
        }
    }

    @RequestMapping(value = "check",method = RequestMethod.POST)
    public ResultVO check(String username){
        if(username == null || username.trim().length() == 0){
            return ResultVO.serviceFail("用户名不能为空");
        }
       boolean notExist =  userService.checkUsername(username);
        if(notExist){
            return ResultVO.success("用户名不存在");
        }else {
            return ResultVO.serviceFail("用户名已存在");
        }
    }

    @RequestMapping(value = "logout",method = RequestMethod.POST)
    public ResultVO logout(){

        return ResultVO.success("用户退出成功");
    }

    @RequestMapping(value = "getUserInfo",method = RequestMethod.GET)
    public ResultVO getUserInfo(){

        String uuid = CurrentUser.getUserId();
        if(uuid == null || uuid.trim().length() == 0){
            return ResultVO.serviceFail("用户未登录");
        }else {
            Integer id = Integer.parseInt(uuid);
            UserInfoModel model = userService.getUserInfo(id);
            if(model == null){
                return ResultVO.serviceFail("用户信息查询失败");
            }else {
                return ResultVO.success(model);
            }
        }

    }

    @RequestMapping(value = "updateUserInfo",method = RequestMethod.POST)
    public ResultVO updateUserInfo(){

        String uuid = CurrentUser.getUserId();
        if(uuid == null || uuid.trim().length() == 0){
            return ResultVO.serviceFail("用户未登录");
        }else {
            Integer id = Integer.parseInt(uuid);
            UserInfoModel model = userService.getUserInfo(id);
            if(model == null){
                return ResultVO.serviceFail("用户信息查询失败");
            }else {
                if(model.getUuid() == id){

                    UserInfoModel model1 = userService.updateUserInfo(model);
                    if(model1 != null)
                      return ResultVO.success(model);
                    else
                        return ResultVO.serviceFail("用户信息更新失败");
                }else {
                    return ResultVO.serviceFail("非当前用户");
                }

            }
        }

    }
}
